export type NavigateType = "navigateTo" | "switchTab" | "reLaunch";
/**
 * 跳转到指定页面
 * @param url - 跳转的url
 * @param target - 跳转方式，可选值：navigateTo、switchTab、reLaunch 默认为navigateTo
 */
export const goto = (url: string, target: NavigateType = "navigateTo") => {
  if (url === undefined || url === "") return;
  if (target === "reLaunch") {
    uni.reLaunch({ url });
  } else if (target === "switchTab") {
    uni.switchTab({ url });
  } else {
    uni.navigateTo({ url });
  }
};

/**
 * ### 显示成功提示信息
 *
 * @ param title - 提示信息
 * @ param delta - 返回的页面层级，默认-1，表示停留当前页面
 * @ param duration - 显示时间，默认500ms
 *
 * */
export const successedToast = (
  title: string = "操作成功",
  delta: number = -1,
  duration: number = 500
) => {
  uni.showToast({
    mask: true,
    title,
    icon: "success",
    duration,
    success: () => {
      if (delta === -1) return;
      setTimeout(() => {
        uni.navigateBack({ delta });
      }, duration);
    },
  });
};

export const showErrorToast = (
  title: string = "操作失败",
  duration: number = 500
) => {
  uni.showToast({
    mask: true,
    title,
    icon: "none",
  });
};

/** 努力开发中 */
export const underDev = () => {
  uni.showToast({
    title: "功能开发中，敬请期待！",
    icon: "none",
  });
};

// 金额限制
export const limitMoney = (value: string) => {
  return value
    .replace(/[^\d.]/g, "")
    .replace(/\.{2,}/g, ".")
    .replace(/^0+/g, "0")
    .replace(/^0+\./g, "0.")
    .replace(/^0+\d/g, "0")
    .replace(/^(\d+)\.(\d\d).*$/, "$1.$2");
};

// 整数限制
export const limitInteger = (value: string) => {
  return value
    .replace(/[^\d]/g, "")
    .replace(/^0+/g, "0")
    .replace(/^0+\d/g, "0");
};
